package com.xingxue.logistics.controller.sys;

import com.xingxue.logistics.common.vo.MenuTreeNode;
import com.xingxue.logistics.common.vo.RestResult;
import com.xingxue.logistics.common.vo.TreeNode;
import com.xingxue.logistics.common.vo.UserRoleNode;
import com.xingxue.logistics.common.vo.UserTreeNode;
import com.xingxue.logistics.service.LimitService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Kris at 2018/10/9 17:06
 */
@RestController
@RequestMapping("sys/limit")
@Api(description = "权限相关控制器")
public class LimitController {

    @Autowired
    LimitService limitService;

    @ApiOperation("根据角色加载用户树(角色授权)")
    @GetMapping("role_user_menu/{roleId}")
    public ResponseEntity<List<UserTreeNode>> RoleUserTree(@PathVariable("roleId") @ApiParam("角色ID") Integer roleId){
        List<UserTreeNode> treeNodeList=limitService.RoleUserTree(roleId);
        return ResponseEntity.ok(treeNodeList);
    }

    @ApiOperation("根据角色加载菜单树(角色授权)")
    @GetMapping("role_menu_menu/{roleId}")
    public ResponseEntity<List<MenuTreeNode>> loadRoleMenuTree(@PathVariable("roleId") @ApiParam("角色ID") Integer roleId){
        List<MenuTreeNode> treeNodeList=limitService.loadRoleMenuTree2(roleId);
        return ResponseEntity.ok(treeNodeList);
    }



    @ApiOperation("维护角色菜单权限")
    @PutMapping("role_menu")
    public ResponseEntity<RestResult> saveRoleMenu(@RequestBody SaveRoleMenuReq req){
        if(limitService.saveRoleMenu(req.roleId,req.menuIds,req.checked)){
            return ResponseEntity.ok(new RestResult(true,"操作成功",null));
        }
        return ResponseEntity.ok(new RestResult(false,"操作失败",null));
    }


    @ApiModel("保存角色菜单权限的参数模型")
    public static class SaveRoleMenuReq{
        @ApiModelProperty("角色ID")
        public Integer roleId;
        @ApiModelProperty("菜单ID")
        public List<Integer> menuIds;
        @ApiModelProperty("是否选中")
        public Boolean checked;
    }










    @ApiOperation("根据用户加载角色")
    @GetMapping("role_user/{userId}")
    public ResponseEntity<List<UserRoleNode>> loadRoleUser(@PathVariable("userId") @ApiParam("角色ID") Integer userId){
        List<UserRoleNode> userRoleNodes = limitService.loadRoleUserTree(userId);
        return ResponseEntity.ok(userRoleNodes);
    }

    @ApiOperation("维护角色用户权限")
    @PutMapping("role_user")
    public ResponseEntity<RestResult> saveRoleUser(@RequestBody SaveRoleUserReq req){
        if(limitService.saveRoleUser(req.userId,req.roleIds,req.checked)){
            return ResponseEntity.ok(new RestResult(true,"操作成功",null));
        }
        return ResponseEntity.ok(new RestResult(false,"操作失败",null));
    }


    @ApiModel("保存角色菜单权限的参数模型")
    public static class SaveRoleUserReq{
        @ApiModelProperty("用户ID")
        public Integer userId;
        @ApiModelProperty("角色ID")
        public List<Integer> roleIds;
        @ApiModelProperty("是否选中")
        public Boolean checked;
    }



}
